from view import View
from flask import Flask, Markup, redirect, url_for

# 接收模块名字作为参数
app = Flask(__name__)


# 多种路由接收首页，且直接转向到CPU占用率视图
@app.route('/')
@app.route('/index')
@app.route('/home')
def index():
    # redirect()函数使得http转向
    # url_for()获得url路径
    return redirect(url_for("cpu"))


# 装饰器将URL和函数绑定，只接收GET方法
@app.route('/cpu', methods=['GET'])
def cpu():
    v = View()
    c = v.cpu_line()
    # render_embed()渲染图表并输出HTML字符串
    # 防止XSS攻击，Markup()对HTML安全标记，并将其转化为str类型
    return Markup(c.render_embed())


@app.route('/memo', methods=['GET'])
def memo():
    v = View()
    # 这里只需要图表，不需要明细数据
    c, val = v.memory_gauge()
    return Markup(c.render_embed())


if __name__ == "__main__":
    # host=127.0.0.1
    # port=5000
    app.run(debug=False)
